<?php
namespace app\models;

use \Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

/**
 *
 * @author a761208
 *        
 * @property integer id
 * @property string email
 * @property string mobile
 * @property string salt
 * @property string password
 */
class User extends ActiveRecord implements IdentityInterface {

	/**
	 * @inheritdoc
	 */
	public static function findIdentity($id) {
		return self::findOne($id);
	}

	/**
	 * @inheritdoc
	 */
	public static function findIdentityByAccessToken($token, $type = null) {
		return null;
	}

	/**
	 * Finds user by username
	 *
	 * @param string $username        	
	 * @return static|null
	 */
	public static function findByUsername($username) {
		return self::findOne([
			'mobile'=>$username
		]);
	}

	/**
	 * @inheritdoc
	 */
	public function getId() {
		return $this->id;
	}

	/**
	 * @inheritdoc
	 */
	public function getAuthKey() {
		return $this->password;
	}

	/**
	 * @inheritdoc
	 */
	public function validateAuthKey($authKey) {
		return $this->getAuthKey() === $authKey;
	}

	/**
	 * Validates password
	 *
	 * @param string $password
	 *        	password to validate
	 * @return boolean if password provided is valid for current user
	 */
	public function validatePassword($password) {
		return $this->password === $this->hashPassword($this->salt, $password);
	}

	public function hashPassword($salt, $password) {
		return md5($salt . $password);
	}

	public function getUsername() {
		return $this->mobile;
	}
}
